import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../store/index';

Component({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            unReadCount: 'unReadCount',
            tabBar: 'tabBar',
        },
        actions: {
            setTabBar: 'setTabBar',

        }
    },
    /**
     * 组件的初始数据
     */
    data: {

        list: [{
                "pagePath": "/pages/home/index",
                "iconPath": "/static/images/tabbar/home.png",
                "selectedIconPath": "/static/images/tabbar/home-active.png",
                "text": "首页"
            },
            {
                "pagePath": "/pages/classify/index",
                "iconPath": "/static/images/tabbar/classify.png",
                "selectedIconPath": "/static/images/tabbar/classify-active.png",
                "text": "分类"
            },
            {
                "pagePath": "/pages/cart/index",
                "iconPath": "/static/images/tabbar/cart.png",
                "selectedIconPath": "/static/images/tabbar/cart-active.png",
                "text": "购物车",
                info: 0
            },
            {
                "pagePath": "/pages/my/index",
                "iconPath": "/static/images/tabbar/my.png",
                "selectedIconPath": "/static/images/tabbar/my-active.png",
                "text": "我的"
            }
        ],
        tabBar: 0

    },
    options: {
        styleIsolation: 'shared'
    }, //样式隔离
    observers: {
        unReadCount(val) {
            let count = val
            if (val > 99) {
                count = '99+'
            }
            this.setData({
                'list[2].info': count
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        onChange(event) {
            let tab = event.detail
            this.setTabBar(tab)
            wx.switchTab({ url: this.data.list[tab].pagePath })
        }
    }
})